package com.ciaojian.client.service;


import com.ciaojian.core.mapper.UserMapper;
import com.ciaojian.core.model.DistUser;
import com.ciaojian.core.model.User;
import com.ciaojian.core.model.vo.UserLoginInfo;
import com.ciaojian.core.service.UserLoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Atlakyn
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private DistUserServiceImpl distUserService;

    /**
     * 根据 unionId 更新
     *
     * @param user user 对象
     */
    public void updateUserByUnionId(User user) {
        userMapper.updateUserByUnionId(user);
    }

    @Override
    public User getUserByUnionId(String unionId) {
        return userMapper.getDUserList(unionId);
    }

    @Override
    public int insert(User user) {
        return userMapper.getAddWxUser(user) > 0 ? 1 : 0;
    }

    /**
     * 登录成功返回登录信息
     *
     * @param uid 用户ID
     * @return 登录信息
     */
    public UserLoginInfo loginSuccess(Integer uid) {
        UserLoginInfo userLoginInfo = new UserLoginInfo();
        DistUser distUser = distUserService.getById(uid);

        userLoginInfo.setUserId(uid);
        userLoginInfo.setDistUser(distUser);
        return userLoginInfo;
    }
}
